package com.platform.core.web.controller;

import com.platform.comm.validation.groups.Insert;
import com.platform.comm.validation.groups.Modify;
import com.platform.comm.validation.groups.Pwd;
import com.platform.core.security.auth.AuthenticationFacade;
import com.platform.core.service.SysUserService;
import com.platform.dto.ExternalUserQo;
import com.platform.dto.PwdQo;
import com.platform.dto.UserQo;
import com.platform.comm.web.results.DataJsonResult;
import com.platform.comm.web.results.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * Description: 用户管理
 *
 * @author lch
 * Created on 2020/10/15
 **/
@RestController
@RequestMapping("/management/shareUser")
@Slf4j
public class ShareUserController extends BaseController {

    private final SysUserService sysUserService;

    public ShareUserController(SysUserService sysUserService) {
        this.sysUserService = sysUserService;
    }

    @PostMapping(name = "新增用户", path = "create")
    public JsonResult create(@Validated({Insert.class}) @RequestBody ExternalUserQo user) {
        sysUserService.shareCreate(user);
        return JsonResult.success();
    }

    @PostMapping(name = "修改用户", path = "modify")
    public JsonResult modify(@Validated({Modify.class}) @RequestBody UserQo user) {
        return createOrModifyWithResult(sysUserService::modify, user);
    }

    @GetMapping(name = "获取用户信息", path = "userInfo")
    public DataJsonResult detail() {
        String userId = AuthenticationFacade.getUserId();
        return DataJsonResult.succeed(sysUserService.getByShareUserId(userId));
    }

    @PostMapping(name = "修改用户密码", path = "modifyPwd")
    public JsonResult modifyPwd(@Validated(Pwd.class) @RequestBody PwdQo pwd) {
        return modify(sysUserService::modifyPwd, AuthenticationFacade.getUserName(), pwd);
    }
}
